# listings/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser, Skill, Need # 现在这些模型应该可以被正确导入了
from .forms import CustomUserCreationForm, CustomUserChangeForm

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['username', 'email', 'student_id', 'college', 'user_type', 'is_staff', 'is_active']
    list_filter = ['user_type', 'is_staff', 'is_active', 'college']
    search_fields = ['username', 'email', 'student_id', 'first_name', 'last_name']
    ordering = ['username']
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        ('Personal info', {'fields': ('first_name', 'last_name', 'email')}),
        ('Additional Info', {'fields': ('user_type', 'student_id', 'college', 'major', 'bio')}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
        ('Important dates', {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'first_name', 'last_name',
                       'user_type', 'student_id', 'college', 'major', 'bio',
                       'password', 'password2')}
        ),
    )

class SkillAdmin(admin.ModelAdmin):
    list_display = ('title', 'provider', 'category', 'reward_type', 'created_at', 'is_active')
    list_filter = ('category', 'reward_type', 'is_active', 'provider')
    search_fields = ('title', 'description', 'provider__username') # 可以搜索关联字段
    date_hierarchy = 'created_at' # 添加日期层级导航
    ordering = ('-created_at',)
    # fields = (...) # 可以自定义编辑页面的字段顺序和分组
    # readonly_fields = ('created_at', 'updated_at') # 设置只读字段

@admin.register(Need)
class NeedAdmin(admin.ModelAdmin):
    list_display = ['title', 'requester', 'category', 'reward_type', 'created_at', 'updated_at', 'is_active']
    list_filter = ['category', 'reward_type', 'is_active', 'created_at']
    search_fields = ['title', 'description', 'tags']
    date_hierarchy = 'created_at'
    ordering = ('-created_at',)
    # actions = ['mark_as_resolved', 'mark_as_unresolved'] # 可以添加自定义动作

    # def mark_as_resolved(self, request, queryset):
    #     queryset.update(is_resolved=True)
    # mark_as_resolved.short_description = "标记为已解决"

    # def mark_as_unresolved(self, request, queryset):
    #     queryset.update(is_resolved=False)
    # mark_as_unresolved.short_description = "标记为未解决"

# 注册模型到 Admin 站点
admin.site.register(CustomUser, CustomUserAdmin)
admin.site.register(Skill, SkillAdmin) # 现在 SkillAdmin 是定义好的